package com.cheer.wheelview;

import java.text.DecimalFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

/**
 * 时间选择器
 */
public class TimeSelectionDialog extends Dialog {

	// 当前时
	private static int NOW_HOUR;
	// 当前分
	private static int NOW_MINUTE;
	// 记录选择的时
	private static int RECORD_HOUR;
	// 记录选择的分
	private static int RECORD_MINUTE;

	private Context mContext;

	private WheelView wheelHour;

	private WheelView wheelMinute;
	/** 是否是当前时间 */
	private boolean flag;

	public TimeSelectionDialog(Context context, boolean flag) {
		super(context, R.style.user_selected_dialog);
		this.mContext = context;
		this.flag = flag;
		View view = getPickerView();
		setContentView(view);
		setListener(view);

		WindowManager wm = ((Activity) context).getWindowManager();
		Display display = wm.getDefaultDisplay();
		WindowManager.LayoutParams lp = getWindow()
				.getAttributes();
		lp.width = display.getWidth() - DensityUtils.dip2px(context, 40);
		getWindow().setAttributes(lp);
	}

	private View getPickerView() {
		final Calendar mCalendar = Calendar.getInstance();
		NOW_HOUR = mCalendar.get(Calendar.HOUR_OF_DAY);
		NOW_MINUTE = mCalendar.get(Calendar.MINUTE);
		if (flag) {
			RECORD_HOUR = NOW_HOUR;
			RECORD_MINUTE = NOW_MINUTE;
		} else {
			RECORD_HOUR = 0;
			RECORD_MINUTE = 0;
		}

		View view = LayoutInflater.from(mContext).inflate(
				R.layout.dialog_time_pick_layout, null);
		wheelHour = (WheelView) view.findViewById(R.id.hour);
		wheelMinute = (WheelView) view.findViewById(R.id.minute);
		NumericWheelAdapter hourAdapter;
		NumericWheelAdapter minuteAdapter;
		if (flag) {
			hourAdapter = new NumericWheelAdapter(NOW_HOUR, 23);
			minuteAdapter = new NumericWheelAdapter(NOW_MINUTE, 59);
			// 当前项
			wheelHour.setCurrentItem(0);
			wheelMinute.setCurrentItem(0);
		} else {
			hourAdapter = new NumericWheelAdapter(0, 23);
			minuteAdapter = new NumericWheelAdapter(0, 59);
			// 当前项
			wheelHour.setCurrentItem(NOW_HOUR - 1);
			wheelMinute.setCurrentItem(NOW_MINUTE);
		}

		// 时，转轮初始化
		wheelHour.setAdapter(hourAdapter);
		// 设置标签
		wheelHour.setLabel(mContext.getResources().getString(R.string.hour));
		wheelHour.setLableOffest(-(int) mContext.getResources().getDimension(
				R.dimen.pxtodp40));
		// 循环
		wheelHour.setCyclic(false);

		// 分，转轮初始化
		wheelMinute.setAdapter(minuteAdapter);
		wheelMinute
				.setLabel(mContext.getResources().getString(R.string.minute));
		wheelMinute.setLableOffest(-(int) mContext.getResources().getDimension(
				R.dimen.pxtodp30));
		wheelMinute.setCyclic(false);

		wheelHour.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (flag) {
					RECORD_HOUR = NOW_HOUR + newValue;
				} else {
					RECORD_HOUR = newValue;
				}
			}
		});

		wheelMinute.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (flag) {
					RECORD_MINUTE = NOW_MINUTE + newValue;
				} else {
					RECORD_MINUTE = newValue;
				}
			}
		});
		return view;
	}

	public String getTimeString() {
		DecimalFormat df = new DecimalFormat("00");
		String hour = "";
		String minute = "";
		hour = df.format(Integer.valueOf(RECORD_HOUR));
		minute = df.format(Integer.valueOf(RECORD_MINUTE));
		String str_time = hour + ":" + minute;
		return str_time;
	}

	private void setListener(View view) {
		final Button btnSure = (Button) view.findViewById(R.id.btn_time_sure);
		btnSure.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				String selectTime = getTimeString();
				btnSure.setTextColor(0xffffff);
				chooseTimeListener.chooseTime(selectTime, RECORD_HOUR);
				dismiss();
			}
		});

		Button btnCancel = (Button) view.findViewById(R.id.btn_time_cancel);
		btnCancel.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	public ChooseTimeListener chooseTimeListener;

	public void setOnChooseTimeListener(ChooseTimeListener chooseTimeListener) {
		this.chooseTimeListener = chooseTimeListener;
	}

	/**
	 * selectTime 表示所选当前日期 flag 表示所选当前日期是否是今天 true表示今天
	 */
	public interface ChooseTimeListener {
		void chooseTime(String selectTime, int hour);
	}
}
